/**
 * Chesney's Fireplace System Includes New Supplier, New Fireplace Item, Supplier and Fireplace Update, 
 */
package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import Database.DatabaseExtended;
import Database.MyFireplace;
import Database.MySupplier;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



public class FireplaceUpdate extends JFrame {
	
	
	private JPanel contentPane;
	private JTextField unitPriceTxt;
	private JTextField fireplaceFinishTxt;
	private JTextField fireplaceStyleTxt;
	private JTextField fireplaceNameTxt;
	JTextArea fireplaceDescriptionTxt;
	private String id;
	
	JComboBox<MySupplier> supplierNameComboBox;
	// ComboBoxModel<MySupplier> cModel;
	private MySupplier s;

	private MySupplier[] suppliers;
	private String SupplierID;
	DefaultComboBoxModel<MySupplier> model;
	private JTextField fireplaceStockTxt;

	private MyFireplace fireplace;
	private MyFireplace[] fireplaces;
	JComboBox<MyFireplace> fireplaceNameComboBox;
	// ComboBoxModel<MyFireplace> dModel;
	private MyFireplace f;
	private MyFireplace fi;
	private MyFireplace[] fireplaceCombo;
	private String FireplaceID;
	DefaultComboBoxModel<MyFireplace> modelCombo;
	private MyFireplace fireplaceSearch;
	private JFrame thisFrame = this;
	
	
	/**
	 * Create the frame.
	 */
	public FireplaceUpdate() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 435, 619);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setResizable(false);

		// Supplier Combobox stuff
		supplierNameComboBox = new JComboBox<MySupplier>();

		suppliers = DatabaseExtended.getSuppliers();
		model = new DefaultComboBoxModel<MySupplier>();

		//System.out.println("Supplier Size: " + suppliers.length);
		for (MySupplier s : suppliers) {
			model.addElement(s);
			//System.out.println("This is the to string method in supplier"
					//+ s.toString());
		}
		supplierNameComboBox.setModel(model);

		// fireplace Combobox stuff
		fireplaceNameComboBox = new JComboBox<MyFireplace>();

		MyFireplace fireplaces[] = DatabaseExtended.getFireplaces();
		modelCombo = new DefaultComboBoxModel<MyFireplace>();

		//System.out.println("Fireplace Size: " + fireplaces.length);
		for (MyFireplace fi : fireplaces) {
			modelCombo.addElement(fi);
			//System.out.println("This is the to string method in supplier"
					//+ fi.toString());
		}

		fireplaceNameComboBox.setModel(modelCombo);

		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JLabel label = new JLabel("Update Fireplace Item");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				Alignment.TRAILING,
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(label, GroupLayout.DEFAULT_SIZE, 375,
								Short.MAX_VALUE).addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
				Alignment.TRAILING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(label, GroupLayout.DEFAULT_SIZE, 40,
								Short.MAX_VALUE).addContainerGap()));
		panel.setLayout(gl_panel);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));

		JLabel label_1 = new JLabel("Search Fireplace:");

		JButton selectButton = new JButton("Select");
		selectButton.addActionListener(new ActionListener() {
			/**
			 * Set all of the details for a particular fireplace
			 */
			public void actionPerformed(ActionEvent e) {
				
				//Select the fireplace from the comobobox
				MyFireplace fireplace = (MyFireplace) fireplaceNameComboBox
						.getSelectedItem();
				//replace all /n that could interfere
				fireplace.setFireplaceID(fireplace.getFireplaceID()
						.replaceAll("/n", "").trim());
				fireplace.getFireplaceID();
				//Printing for debugging purposes
				//System.out.println("This is the fireplaceID"
						//+ fireplace.getFireplaceID());
				
				//set textfields with the data of the fireplace selected
				//in order to enable the user to update it
				fireplaceNameTxt.setText(fireplace.getFireplaceName());
				// fireplaceDescriptionTxt.setText(fireplace.getDescription());
				fireplaceDescriptionTxt.setText(fireplace.getDescription());
				fireplaceFinishTxt.setText(fireplace.getFinish());
				fireplaceStyleTxt.setText(fireplace.getStyle());
				unitPriceTxt.setText(String.valueOf(fireplace.getItemPrice()));
				fireplaceStockTxt.setText(String.valueOf(fireplace.getStockQuantity()));

				MySupplier supplier = (MySupplier) supplierNameComboBox
						.getSelectedItem();
				//printing for debugging purposes
				//System.out.println("Suppliers Size: " + suppliers.length);
				
				//get the supplier linked to the fireplace selected
				for (MySupplier s : suppliers) {
					//System.out.println("ID: " + s.getSupplierID());
					//s.setSupplierID(s.getSupplierID().replaceAll("/n", "")
							//.trim());
					if (s.getSupplierID().equals(fireplace.getSupplierID())) {

						supplierNameComboBox.setSelectedItem(s);
					}

				}

			}
		});

		JButton deleteButton = new JButton("Delete");
		deleteButton.addActionListener(new ActionListener() {
			/**
			 * Set all of the details ready to be deleted
			 */
			public void actionPerformed(ActionEvent e) {
				//get the item from the fireplace combobox
				MyFireplace fireplace = (MyFireplace) fireplaceNameComboBox.getSelectedItem();
				
				//print data for debugging purposes
				//System.out.println("This is the fireplaceID"
						//+ fireplace.getFireplaceID());
				//replace /n that could be interfering 
				fireplace.setFireplaceID(fireplace.getFireplaceID()
						.replaceAll("/n", "").trim());
				//get the fireplace ID
				int fireplaceID = Integer.valueOf(fireplace.getFireplaceID());
				//set up variable that contains warning dialogue
				int confirmed = JOptionPane.showConfirmDialog(null,
						"Are you sure you want to delete a fireplace?",
						"User Confirmation", JOptionPane.YES_NO_OPTION);
				//if selected yes option....
				if (confirmed == JOptionPane.YES_OPTION) {
					//Use method to delete the fireplace
					DatabaseExtended.deleteFireplace(fireplaceID);
					fireplaceNameComboBox.removeItem(fireplaceNameComboBox
							.getSelectedItem());
					//and clear fields
					fireplaceNameTxt.setText("");
					fireplaceDescriptionTxt.setText("");
					fireplaceFinishTxt.setText("");
					fireplaceStyleTxt.setText("");
					unitPriceTxt.setText("");
					fireplaceStockTxt.setText("");
					supplierNameComboBox.setSelectedIndex(0);

				}

			}
		});

		// JComboBox fireplaceNameComboBox = new JComboBox();
		fireplaceNameComboBox.setMaximumRowCount(50);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1
				.setHorizontalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGap(0, 399, Short.MAX_VALUE)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(label_1)
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				selectButton)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				deleteButton))
														.addComponent(
																fireplaceNameComboBox,
																GroupLayout.PREFERRED_SIZE,
																194,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(98, Short.MAX_VALUE)));
		gl_panel_1
				.setVerticalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGap(0, 91, Short.MAX_VALUE)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_1)
														.addComponent(
																fireplaceNameComboBox,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addComponent(
																deleteButton,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																selectButton,
																GroupLayout.DEFAULT_SIZE,
																31,
																Short.MAX_VALUE))
										.addContainerGap(14, Short.MAX_VALUE)));
		panel_1.setLayout(gl_panel_1);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));

		JLabel label_2 = new JLabel("Stock Quantity:");

		JLabel label_3 = new JLabel("Supplier Name:");

		JLabel label_4 = new JLabel("Unit Price:");

		JLabel label_5 = new JLabel("Style:");

		JLabel label_6 = new JLabel("Finish:");

		JLabel label_7 = new JLabel("Description:");

		JLabel label_8 = new JLabel("Item Name:");

		unitPriceTxt = new JTextField();
		unitPriceTxt.setColumns(10);

		fireplaceFinishTxt = new JTextField();
		fireplaceFinishTxt.setColumns(10);

		fireplaceStyleTxt = new JTextField();
		fireplaceStyleTxt.setColumns(10);

		fireplaceNameTxt = new JTextField();
		fireplaceNameTxt.setColumns(10);

		fireplaceDescriptionTxt = new JTextArea();
		fireplaceDescriptionTxt.setWrapStyleWord(true);
		fireplaceDescriptionTxt.setLineWrap(true);

		// JComboBox supplierNameComboBox = new JComboBox();

		fireplaceStockTxt = new JTextField();
		fireplaceStockTxt.setColumns(10);
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2
				.setHorizontalGroup(gl_panel_2
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_2
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(label_2)
														.addComponent(label_3)
														.addComponent(label_4)
														.addComponent(label_5)
														.addComponent(label_6)
														.addComponent(label_7)
														.addComponent(label_8))
										.addGap(18)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																unitPriceTxt,
																GroupLayout.PREFERRED_SIZE,
																107,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																gl_panel_2
																		.createParallelGroup(
																				Alignment.TRAILING,
																				false)
																		.addComponent(
																				fireplaceFinishTxt,
																				Alignment.LEADING)
																		.addComponent(
																				fireplaceStyleTxt,
																				Alignment.LEADING))
														.addGroup(
																gl_panel_2
																		.createParallelGroup(
																				Alignment.TRAILING,
																				false)
																		.addComponent(
																				fireplaceNameTxt,
																				Alignment.LEADING)
																		.addComponent(
																				fireplaceDescriptionTxt,
																				Alignment.LEADING,
																				GroupLayout.DEFAULT_SIZE,
																				194,
																				Short.MAX_VALUE))
														.addComponent(
																supplierNameComboBox,
																GroupLayout.PREFERRED_SIZE,
																177,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																fireplaceStockTxt,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(98, Short.MAX_VALUE)));
		gl_panel_2
				.setVerticalGroup(gl_panel_2
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_2
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_8)
														.addComponent(
																fireplaceNameTxt,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_7)
														.addComponent(
																fireplaceDescriptionTxt,
																GroupLayout.PREFERRED_SIZE,
																54,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_6)
														.addComponent(
																fireplaceFinishTxt,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_5)
														.addComponent(
																fireplaceStyleTxt,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_4)
														.addComponent(
																unitPriceTxt,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_2)
														.addComponent(
																fireplaceStockTxt,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_3)
														.addComponent(
																supplierNameComboBox,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(15, Short.MAX_VALUE)));
		panel_2.setLayout(gl_panel_2);

		JButton saveButton = new JButton("Save and Submit");
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				//Set up new array that will contain the input data of the screen
				String[] data = new String[8];
				//Set up a Myfireplace object that will contain the fireplace from the database
				MyFireplace fireplace = (MyFireplace) fireplaceNameComboBox.getSelectedItem();
				//if id is not null set up to null
				try {
					data[0] = fireplace.getFireplaceID();
				} catch (Exception e) {
					data[0] = null;
				}
				//Some test useful for debuggin purposes
				
				//System.out.println("This is the fireplace ID" + data[0]);
				//System.out.println("Fireplace ID: " + id);
				
				//get the data from the screen fields and put in the array.
				data[1] = fireplaceNameTxt.getText();
				data[2] = fireplaceDescriptionTxt.getText();
				data[3] = fireplaceFinishTxt.getText();
				data[4] = fireplaceStyleTxt.getText();
				data[5] = unitPriceTxt.getText();
				data[6] = fireplaceStockTxt.getText();
				MySupplier supplier = (MySupplier) supplierNameComboBox
						.getSelectedItem();
				data[7] = supplier.getSupplierID();
				//If price field empty enter 0, otherwise database exception
				if (data[5].equals("")) {
					data[5] = "0";
				}
				//if stock field empty enter 0 otherwise database exception
				if (data[6].equals("")) {
					data[6] = "0";
				}
				//if fireplace name empty warning dialogue
				if (data[1].equals("")) {
					JOptionPane.showMessageDialog(null,
							"The fireplace name field cannot be empty");
				//otherwise use the svafireplace method to create the new item
				} else{
					try {
						//Use the updateFireplace method to update the item selected
						//by passing a fireplace object and the array with the data
						DatabaseExtended.updateFireplace(new MyFireplace(data));
						//and dispose the frame
						thisFrame.dispose();
					//warning dialogue if enter wrong data type in numerical fields
					} catch (Exception e) {
						JOptionPane
								.showMessageDialog(null,
										"Please enter a numerical value in stock and price field");
					}
				}
				
				
				
			
			}
		});
		
		JButton btnNewButton = new JButton("Main Menu");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//dispose the frame when exit button pressed
				thisFrame.dispose();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 399, GroupLayout.PREFERRED_SIZE)
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 399, GroupLayout.PREFERRED_SIZE)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 399, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(saveButton, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(16, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(19)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
					.addGap(16)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 312, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(saveButton, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(22, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);

	}

}
